import { IParam, isEmpty } from '@/core';
import axios from 'axios';
import qs from 'qs';

export class Http {
	/**
	 * Http对象
	 *
	 * @private
	 * @static
	 * @type {Http}
	 * @memberof Http
	 */
	private static http: Http;

	/**
	 * 获取Http实例
	 *
	 * @static
	 * @return {*}  {Http}
	 * @memberof Http
	 */
	public static getInstance(): Http {
		if (!this.http) {
			this.http = new Http();
		}
		return this.http;
	}

	/**
	 *获取原生请求对象
	 *
	 * @static
	 * @return {*}
	 * @memberof Http
	 */
	public static getHttp() {
		return axios;
	}

	/**
	 * post请求
	 *
	 * @param {string} url
	 * @param {IParam} [params={}]
	 * @param {IParam} [headers={}]
	 * @param {boolean} [isLoading]
	 * @return {*}  {Promise<any>}
	 * @memberof Http
	 */
	public post(
		url: string,
		params: IParam = {},
		headers: IParam = {},
		isLoading?: boolean
	): Promise<any> {
		params = this.handleRequestData(params);
		url = this.handleRequestUrl(url, params);
		return new Promise((resolve: any, reject: any) => {
			axios({
				method: 'post',
				url,
				data: params,
				headers,
			})
				.then((response: any) => {
					this.doResponseResult(response, resolve);
				})
				.catch((response: any) => {
					this.doResponseResult(response, reject);
				});
		});
	}

	/**
	 * put请求
	 *
	 * @param {string} url
	 * @param {IParam} [params={}]
	 * @param {IParam} [headers={}]
	 * @param {boolean} [isLoading]
	 * @return {*}  {Promise<any>}
	 * @memberof Http
	 */
	public put(
		url: string,
		params: IParam = {},
		headers: IParam = {},
		isLoading?: boolean
	): Promise<any> {
		params = this.handleRequestData(params);
		url = this.handleRequestUrl(url, params);
		return new Promise((resolve: any, reject: any) => {
			axios({
				method: 'PUT',
				url,
				data: params,
				headers,
			})
				.then((response: any) => {
					this.doResponseResult(response, resolve);
				})
				.catch((response: any) => {
					this.doResponseResult(response, reject);
				});
		});
	}

	/**
	 * delete请求
	 *
	 * @param {string} url
	 * @param {IParam} [params={}]
	 * @param {IParam} [headers={}]
	 * @param {boolean} [isLoading]
	 * @return {*}  {Promise<any>}
	 * @memberof Http
	 */
	public delete(
		url: string,
		params: IParam = {},
		headers: IParam = {},
		isLoading?: boolean
	): Promise<any> {
		url = this.handleRequestUrl(url, params);
		return new Promise((resolve: any, reject: any) => {
			if (!params) {
				axios({
					method: 'DELETE',
					url,
					headers,
				})
					.then((response: any) => {
						this.doResponseResult(response, resolve);
					})
					.catch((response: any) => {
						this.doResponseResult(response, reject);
					});
			} else {
				axios({
					method: 'DELETE',
					url,
					data: params,
					headers,
				})
					.then((response: any) => {
						this.doResponseResult(response, resolve);
					})
					.catch((response: any) => {
						this.doResponseResult(response, reject);
					});
			}
		});
	}

	/**
	 * get请求
	 * @param url
	 * @param params
	 * @param headers
	 * @param isLoading
	 * @returns
	 */
	public get(
		url: string,
		params: IParam = {},
		headers: IParam = {},
		isLoading?: boolean
	): Promise<any> {
		params = this.handleRequestData(params);
		if (params.srfparentdata) {
			Object.assign(params, params.srfparentdata);
			delete params.srfparentdata;
		}
		if (Object.keys(params).length > 0) {
			const tempParam: any = {};
			Object.keys(params).forEach((item: any) => {
				if (params[item] || Object.is(params[item], 0)) {
					tempParam[item] = params[item];
				}
			});
			if (Object.keys(tempParam).length > 0) {
				url += `?${qs.stringify(tempParam)}`;
			}
		}
		return new Promise((resolve: any, reject: any) => {
			axios({
				method: 'get',
				url,
				headers,
			})
				.then((response: any) => {
					this.doResponseResult(response, resolve);
				})
				.catch((response: any) => {
					this.doResponseResult(response, reject);
				});
		});
	}

	/**
	 * 处理请求数据
	 *
	 * @private
	 * @param {IParam} [params={}]
	 * @return {*}  {IParam}
	 * @memberof Http
	 */
	private handleRequestData(params: IParam = {}): IParam {
		if (!params || Object.keys(params).length === 0) {
			return {};
		}
		if (params.hasOwnProperty('srfsessionkey')) {
			delete params.srfsessionkey;
		}
		if (params.hasOwnProperty('srfsessionid')) {
			delete params.srfsessionid;
		}
		if (params.hasOwnProperty('srfparentdemapname')) {
			delete params.srfparentdemapname;
		}
		return params;
	}

	/**
	 * 处理请求路径
	 *
	 * @private
	 * @param {string} url
	 * @param {IParam} [params={}]
	 * @return {*}  {string}
	 * @memberof Http
	 */
	private handleRequestUrl(url: string, params: IParam = {}): string {
		if (params && Object.keys(params).length > 0) {
			const tempParam: any = {};
			Object.keys(params).forEach((item: string) => {
				if (item.startsWith('srf') && !isEmpty(params[item])) {
					tempParam[item] = params[item];
				}
			});
			// 过滤前端标识属性
			if (tempParam && Object.is(tempParam['srfinsttag'], '__srfstdinst__')) {
				delete tempParam['srfinsttag'];
			}
			if (tempParam && Object.keys(tempParam).length > 0) {
				url += `?${qs.stringify(tempParam)}`;
			}
			return url;
		}
		return url;
	}

	/**
	 * 处理响应结果
	 *
	 * @private
	 * @param {*} response
	 * @param {Function} callback
	 * @memberof Http
	 */
	private doResponseResult(response: any, callback: Function) {
		if (response.status === 200) {
			response.ok = true;
		}
		callback(response);
	}
}
